package cn.com.dhcc.app.pub.core.util.settlement.compute.task;

import cn.com.dhcc.app.pub.core.util.settlement.compute.store.DataGroupHandler;
import cn.com.dhcc.app.pub.core.util.settlement.compute.store.DataGroupVo;
import cn.com.dhcc.app.pub.core.util.settlement.compute.store.DataProcessHandler;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;

/**
 * 数据结算任务--以一次任务为单元进行结算
 * 
 * @author Administrator
 * 
 */
public class DataComputeTask implements Callable<Integer> {

	private String taskId;// 发送任务id
	private String table;// 发送任务id
	private Map<String, List<String>> datas;// 要结算的数据
	private Date date;// 结算数据日期

	private DataGroupHandler groupHandler;

	private DataProcessHandler processHandler;

	public DataComputeTask(String taskId, String table,
			Map<String, List<String>> datas, Date date,
			DataGroupHandler groupHandler, DataProcessHandler processHandler) {
		super();
		this.taskId = taskId;
		this.table = table;
		this.datas = datas;
		this.date = date;
		this.groupHandler = groupHandler;
		this.processHandler = processHandler;
	}

	@Override
	public Integer call() throws Exception {
		int sum = 0;
		try {
			System.out.println("taskId[" + taskId + "]结算开始");
			// 循环表，按照表结算
			Set<String> tables = datas.keySet();
			for (String table : tables) {
				// 先分组--再结算
				System.out.println("开始结算taskId[" + taskId + "],table[" + table + "]");
				List<String> list = datas.get(table);
				DataGroupVo vo = groupHandler.group(list, true);// 需要存储映射信息
				sum = processHandler.compute(taskId, table, vo, date);
				System.out.println("结算结束taskId[" + taskId + "],table[" + table + "]");
			}
			System.out.println("taskId[" + taskId + "]结算结束");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sum;
	}

}
